<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"
%><%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"
%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"
%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%>
<head>
    <title><fmt:message key="contentlet" /></title>
</head>
<body>
    <h1><fmt:message key="contentlet" /></h1>
    <c:url var="createUrl" value="/domain/contentlet/create.action"/>
	<c:url var="treeUrl" value="/domain/rest/folder/children" />

    <form:form action="${createUrl}" id="contentletForm" modelAttribute="contentletForm" enctype="multipart/form-data">
        <form:errors cssClass="error"/>
        <table>
            <form:hidden path="contentlet.inode"/>
            <tbody>
                <tr>
                    <th><fmt:message key="contentlet_structureInode" /></th>
                    <td>
                        <form:hidden path="contentlet.structureInode" />
						${contentletForm.contentlet.structure.inode} -- ${contentletForm.contentlet.structure.name}
                    </td>
				</tr>

                <tr>
                    <th><fmt:message key="contentlet_title" /></th>
                    <td>
                        <form:input path="contentlet.title"/>
                        <script type="text/javascript">
                            Spring.addDecoration(new Spring.ElementDecoration({
                                elementId : "contentlet.title",
                                widgetType : "dijit.form.ValidationTextBox",
                                widgetAttrs : {
                                    required: true
                                }
                            }));
                        </script>
                        <form:errors cssClass="error" path="contentlet.title"/>
                    </td>
                    <th><fmt:message key="contentlet_folderInode" /> *</th>
                    <td>
                        <form:select path="contentlet.folderInode">
                            <form:options items="${folders}" itemValue="inode" itemLabel="leveledName" />
                        </form:select>
                        <script type="text/javascript">
                            Spring.addDecoration(new Spring.ElementDecoration({
                                elementId : "contentlet.folderInode",
                                widgetType : "dijit.form.FilteringSelect",
                                widgetAttrs : {
                                    required: true
                            }}));
                        </script>
                    </td>
                </tr>
                <tr>
                    <th><fmt:message key="contentlet_salePrice" />*</th>
                    <td>
                        <form:input path="contentlet.salePrice"/>
                        <script type="text/javascript">
                            Spring.addDecoration(new Spring.ElementDecoration({
                                elementId : "contentlet.salePrice",
                                widgetType : "dijit.form.ValidationTextBox",
                                widgetAttrs : {
                                    required: true
                                }
                            }));
                        </script>
                        <form:errors cssClass="error" path="contentlet.salePrice"/>
                    </td>
                    <th><fmt:message key="contentlet_listPrice" />*</th>
                    <td>
                        <form:input path="contentlet.listPrice"/>
                        <script type="text/javascript">
                            Spring.addDecoration(new Spring.ElementDecoration({
                                elementId : "contentlet.listPrice",
                                widgetType : "dijit.form.ValidationTextBox",
                                widgetAttrs : {
                                    required: true
                                }
                            }));
                        </script>
                        <form:errors cssClass="error" path="contentlet.listPrice"/>
                    </td>
                </tr>

                <tr>
                    <th><fmt:message key="contentlet_fileAsset" />*</th>
                    <td>
						<input type="file" name="coverFileAsset" value="..." />
                        <form:errors cssClass="error" path="contentlet.fileAsset"/>
                    </td>
                    <th><fmt:message key="contentlet_iconInode" />*</th>
                    <td>
						<input type="file" name="iconFileAsset" value="..." />
                        <form:errors cssClass="error" path="contentlet.iconInode"/>
                    </td>
                </tr>
				<tr>
                    <th><fmt:message key="contentlet_friendlyName" /></th>
                    <td colspan="3">
                        <form:input path="contentlet.friendlyName"/>
                        <script type="text/javascript">
                            Spring.addDecoration(new Spring.ElementDecoration({
                                elementId : "contentlet.friendlyName",
                                widgetType : "dijit.form.SimpleTextarea",
                                widgetAttrs : {
                                    required: true
                                }
                            }));
                        </script>
                        <form:errors cssClass="error" path="contentlet.friendlyName"/>
                    </td>
				
				</tr>
				<c:forEach items="${fields}" var="fieldWrapper" varStatus="status">
				<c:if test="${status.count % 2 == 1}">  
				<tr>  
				</c:if>    
					<th><c:out value="${fieldWrapper.field.fieldName}" /><c:if test="${fieldWrapper.field.required}">*</c:if></th>
					<td>
					<c:choose>
						<c:when test="${fieldWrapper.field.fieldType == 'text'}">

						<c:set value="" var="regExp" /> 
						<c:if test="${not empty fieldWrapper.field.regexCheck}">
							<c:set value="regExp:'${fieldWrapper.field.regexCheck}'," var="regExp" /> 
						</c:if>
						<input type="text" name="contentlet.${fieldWrapper.field.fieldContentlet}" id="${fieldWrapper.field.velocityVarName}" value="${fieldWrapper.fieldValue}" />
						<script type="text/javascript">
							Spring.addDecoration(new Spring.ElementDecoration({
								elementId : "${fieldWrapper.field.velocityVarName}",
								widgetType : "dijit.form.ValidationTextBox",
								widgetAttrs : {
									${regExp}
									required: ${fieldWrapper.field.required != null ? fieldWrapper.field.required: false}
								}
							}));
						</script>
						</c:when>
						<c:when test="${fieldWrapper.field.fieldType == 'textarea'}">
						<input type="text" name="contentlet.${fieldWrapper.field.fieldContentlet}" id="${fieldWrapper.field.velocityVarName}" value="${fieldWrapper.fieldValue}" />
						<script type="text/javascript">
							Spring.addDecoration(new Spring.ElementDecoration({
								elementId : "${fieldWrapper.field.velocityVarName}",
								widgetType : "dijit.form.SimpleTextarea",
								widgetAttrs : {
									required: ${fieldWrapper.field.required != null ? fieldWrapper.field.required : false}
								}
							}));
						</script>
						</c:when>
						<c:when test="${fieldWrapper.field.fieldType == 'category'}">
						<select name="contentlet.${fieldWrapper.field.fieldContentlet}" id="${fieldWrapper.field.velocityVarName}">
							<!-- Find the key which hold the options value. -->
							<c:forEach items="${categoryMap}" var="map" >
								<c:if test="${map.key == fieldWrapper.field.velocityVarName}">
									<c:if test="${fieldWrapper.field.required == null || !fieldWrapper.field.required}">
										<option value="-1" selected>请选择</option>
									</c:if>
									<c:forEach items="${map.value}" var="value">
										<c:if test="${value.key == fieldWrapper.fieldValue}">
										<option value="${value.key}" selected>${value.value}</option>
										</c:if>
										<c:if test="${value.key != fieldWrapper.fieldValue}">
										<option value="${value.key}">${value.value}</option>
										</c:if>
									</c:forEach>
								</c:if>
							</c:forEach>
						</select>
						<c:if test="${fieldWrapper.field.required}">*</c:if>
						<script type="text/javascript">
							Spring.addDecoration(new Spring.ElementDecoration({
								elementId : "${fieldWrapper.field.velocityVarName}",
								widgetType : "dijit.form.FilteringSelect",
								widgetAttrs : {
									required: ${fieldWrapper.field.required != null ? fieldWrapper.field.required : false}
								}
							}));
						</script>
						</c:when>

						<c:when test="${fieldWrapper.field.fieldType == 'multicategory'}">
						<select name="contentlet.${fieldWrapper.field.fieldContentlet}" id="${fieldWrapper.field.velocityVarName}">
							<!-- Find the key which hold the options value. -->
							<c:forEach items="${categoryMap}" var="map" >
								<c:if test="${map.key == fieldWrapper.field.velocityVarName}">
									<c:forEach items="${map.value}" var="value">
										<c:if test="${value.key == fieldWrapper.fieldValue}">
										<option value="${value.key}" selected>${value.value}</option>
										</c:if>
										<c:if test="${value.key != fieldWrapper.fieldValue}">
										<option value="${value.key}">${value.value}</option>
										</c:if>
									</c:forEach>
								</c:if>
							</c:forEach>
						</select>
						<c:if test="${fieldWrapper.field.required}">*</c:if>
						<script type="text/javascript">
							Spring.addDecoration(new Spring.ElementDecoration({
								elementId : "${fieldWrapper.field.velocityVarName}",
								widgetType : "dijit.form.MultiSelect",
								widgetAttrs : {
									required: ${fieldWrapper.field.required != null ? fieldWrapper.field.required : false}
								}
							}));
						</script>
						</c:when>

						<c:when test="${fieldWrapper.field.fieldType == 'checkbox'}">
						<input type="checkbox" name="contentlet.${fieldWrapper.field.fieldContentlet}" id="${fieldWrapper.field.velocityVarName}" value="true">
						<script type="text/javascript">
							Spring.addDecoration(new Spring.ElementDecoration({
								elementId : "${fieldWrapper.field.velocityVarName}",
								widgetType : "dijit.form.CheckBox",
								widgetAttrs : {
								}
							}));
						</script>
						</c:when>


						<c:when test="${fieldWrapper.field.fieldType == 'date'}">
						<input type="text" name="contentlet.${fieldWrapper.field.fieldContentlet}" id="${fieldWrapper.field.velocityVarName}">
						<script type="text/javascript">
							Spring.addDecoration(new Spring.ElementDecoration({
								elementId : "${fieldWrapper.field.velocityVarName}",
								widgetType : "dijit.form.DateTextBox",
								widgetAttrs : {
									//label: '${fieldWrapper.field.fieldValues}'
								}
							}));
						</script>
						</c:when>
					</c:choose>
					</td>
				<c:if test="${((status.count)) % 2 == 0}">  
				</tr>  
				</c:if>  
				</c:forEach>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="4">
                        <div class="buttons">
                            <button id="saveButton" class="ok" type="submit"><fmt:message key="label.form.submit"/></button>
                            <script type="text/javascript">
                                Spring.addDecoration(new Spring.ValidateAllDecoration({
                                    elementId:'saveButton',
                                    event:'onclick'
                                }));
                            </script>
                        </div>
                    </td>
                </tr>
            </tfoot>
        </table>
    </form:form>
</body>
